package org.dromara.bean.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.bean.config.AlipayConfig;
import org.dromara.bean.constant.CommonConstant;
import org.dromara.bean.domain.pay.OrderModel;
import org.dromara.bean.service.IBasePaymentService;
import org.dromara.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@Slf4j
@RequiredArgsConstructor
public class PaymentController {

    private final IBasePaymentService basePaymentService;


    private final AlipayConfig alipayConfig;


    public R<?> pay(String payType, OrderModel orderModel) {

        orderModel.setTerminalType("terminal-type");
        if (CommonConstant.WX_PAY.equals(payType)) {
            //微信支付
            orderModel.setPayType(Integer.parseInt(CommonConstant.WX_PAY));
            return wxPay(orderModel);
        }else {
            throw new RuntimeException("支付类型错误");
        }

//         else if (CommonConstant.ALIPAY_PAY.equals(payType)) {
//            //支付宝支付
//            orderModel.setPayType(Integer.parseInt(CommonConstant.ALIPAY_PAY));
//            return wxPay(orderModel);
//        }
    }

    /**
     * 微信统一支付
     *
     * @param orderModel
     * @return
     */
    private R wxPay(OrderModel orderModel) {
        try {
            return basePaymentService.wxPay(orderModel);
        } catch (Exception e) {
            log.error("微信统一支付异常", e);
            throw new RuntimeException("微信统一支付异常");
        }
    }
}
